<?php

App::uses('AppController', 'Controller');

/**
 * Media Controller
 *
 * @property Media $Media
 */
class MediaController extends AppController {

    public function beforeFilter() {
        parent::beforeFilter();
        $this->Auth->allow('*');
    }

    /**
     * admin_index method
     *
     * @return void
     */
    public function admin_index() {
        $this->Media->recursive = 0;
        $this->set('media', $this->paginate());
    }

    /**
     * admin_view method
     *
     * @param string $id
     * @return void
     */
    public function admin_view($id = null) {
        $this->Media->id = $id;
        if (!$this->Media->exists()) {
            throw new NotFoundException(__('Invalid media'));
        }
        $this->set('media', $this->Media->read(null, $id));
    }

    /**
     * admin_add method
     *
     * @return void
     */
    public function admin_add() {
        if ($this->request->is('post')) {
            $this->Media->create();
            if ($this->Media->save($this->request->data)) {
                $this->Session->setFlash(__('The media has been saved'));
                $this->redirect(array('action' => 'index'));
            } else {
                $this->Session->setFlash(__('The media could not be saved. Please, try again.'));
            }
        }
        $articles = $this->Media->Article->find('list');
        $this->set(compact('articles'));
    }

    /**
     * admin_edit method
     *
     * @param string $id
     * @return void
     */
    public function admin_edit($id = null) {
        $this->Media->id = $id;
        if (!$this->Media->exists()) {
            throw new NotFoundException(__('Invalid media'));
        }
        if ($this->request->is('post') || $this->request->is('put')) {
            if ($this->Media->save($this->request->data)) {
                $this->Session->setFlash(__('The media has been saved'));
                $this->redirect(array('action' => 'index'));
            } else {
                $this->Session->setFlash(__('The media could not be saved. Please, try again.'));
            }
        } else {
            $this->request->data = $this->Media->read(null, $id);
        }
        $articles = $this->Media->Article->find('list');
        $this->set(compact('articles'));
    }

    /**
     * admin_delete method
     *
     * @param string $id
     * @return void
     */
    public function admin_delete($id = null) {
        if (!$this->request->is('post')) {
            throw new MethodNotAllowedException();
        }
        $this->Media->id = $id;
        if (!$this->Media->exists()) {
            throw new NotFoundException(__('Invalid media'));
        }
        if ($this->Media->delete()) {
            $this->Session->setFlash(__('Media deleted'));
            $this->redirect(array('action' => 'index'));
        }
        $this->Session->setFlash(__('Media was not deleted'));
        $this->redirect(array('action' => 'index'));
    }

    public function admin_changeFile($id) {
        $this->Media->id = $id;
        if (!$this->Media->exists()) {
            throw new NotFoundException(__('Invalid article file.'));
        }
        if ($this->request->is('post') || $this->request->is('put')) {
            if ($this->Media->save($this->request->data)) {
                $this->Session->setFlash(__('The file has been saved'));
                $this->redirect(array('controller' => 'articles', 'action' => 'edit', $this->request->data['Media']['article_id'], 'admin' => true));
            } else {
                $this->Session->setFlash(__('The file could not be saved. Please, try again.'));
            }
        } else {
            $this->request->data = $this->Media->read(null, $id);
        }
    }

    public function admin_viewFile($id) {
        //$this->autoRender = false;
        
        $this->autoLayout = false;
        
        $this->Media->id = $id;
        if (!$this->Media->exists()) {
            throw new NotFoundException(__('Invalid media'));
        }
        $this->Media->recursive = -1;
        $media = $this->Media->read(null, $id);

        if (empty($media)) {
            $this->redirect('/', 404, true);
        }
        

         $this->viewClass = 'Media';    
         $params = array(          
             'id' => $media['Media']['filename'],          
             'name' => $media['Media']['filename'],                      
             'download' => false,
             'mimeType' => $media['Media']['mimetype'],
              'path' => WWW_ROOT . $media['Media']['dir']  .DS   
           );  
         
         $this->set($params);
        
	
        
    }
    public function admin_downloadFile($id) {
        //$this->autoRender = false;
        
        $this->autoLayout = false;
        
        $this->Media->id = $id;
        if (!$this->Media->exists()) {
            throw new NotFoundException(__('Invalid media'));
        }
        $this->Media->recursive = -1;
        $media = $this->Media->read(null, $id);

        if (empty($media)) {
            $this->redirect('/', 404, true);
        }
        

         $this->viewClass = 'Media';    
         $params = array(          
             'id' => $media['Media']['filename'],          
             'name' => $media['Media']['filename'],                      
             'download' => true,
             'mimeType' => $media['Media']['mimetype'],
              'path' => WWW_ROOT . $media['Media']['dir']  .DS   
           );  
         
         $this->set($params);
        
	
        
    }
    public function download($id) {
        $this->autoLayout = false;
        
        $this->Media->id = $id;
        if (!$this->Media->exists()) {
            throw new NotFoundException(__('Invalid media'));
        }
        $this->Media->recursive = -1;
        $media = $this->Media->read(null, $id);

        if (empty($media)) {
            $this->redirect('/', 404, true);
        }
        

         $this->viewClass = 'Media';    
         $params = array(          
             'id' => $media['Media']['filename'],          
             'name' => $media['Media']['filename'],                      
             'download' => true,
             'mimeType' => $media['Media']['mimetype'],
              'path' => WWW_ROOT . $media['Media']['dir']  .DS   
           );  
         
         $this->set($params);
    }

}
